// pages/classic/classic.js
import {Classic} from '../../models/classic'
import {LikeModel} from "../../models/like"


const classic = new Classic()
const likeModel = new LikeModel()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        first: false,
        latest: true,

        classicData: null,
        likeCount: 0,
        likeStatus: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        classic.getLatest().then(
            (res) => {

                this.setData({
                        classicData: res,
                        likeCount: res.fav_nums,
                        likeStatus: res.like_status
                    }
                )
            }
        )


    },

    onLike: function (event) {
        let behavior = event.detail.behavior
        likeModel.like(behavior, this.data.classicData.id, this.data.classicData.type)

    },

    _updateClassic: function (status) {
        let lIndex = this.data.classicData.index
        classic.getClassic(lIndex, status).then(
            (res) => {
                this._getLikeStatus(res.id, res.type)
                this.setData(
                    {
                        classicData: res,
                        latest: classic.isLatest(res.index),
                        first: classic.isFirst(res.index)
                    }
                )
            }
        )
    },
    _getLikeStatus: function (artID, category) {
        likeModel.getClassicLikeStatus(artID, category,
            (res) => {
                this.setData({
                    likeCount: res.fav_nums,
                    likeStatus: res.like_status
                })


            })
    },


    onNext: function (event) {
        this._updateClassic("next")


    },
    onPrevious: function (event) {
        this._updateClassic("previous")
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
